package com.hai.qtalk;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

import com.hai.bean.MyExpandableListAdapter;
import com.hai.util.ClientThread;
import com.hai.util.HttpUtil;

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
@SuppressWarnings("deprecation")
public class FragmentTabs extends TabActivity {
	private List<String> groupArray;
	private List<List<String>> childArray;
	private ExpandableListView expandableListView;
	EditText input;// 输入文本框
	TextView show;// 聊天消息显示框
	Button send;// 发送按钮
	public static ClientThread clientThread;
	public static Handler handler;//
	private String split = "y9t";

	@SuppressLint("HandlerLeak")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.fragment_main);

		TabHost tabHost = getTabHost();
		// 创建第一个Tab页
		TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("大厅")
				.setContent(R.id.tab01);
		tabHost.addTab(tab1);
		TabSpec tab2 = tabHost.newTabSpec("tab2").setIndicator("好友")
				.setContent(R.id.tab02);
		tabHost.addTab(tab2);
		TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator("文件")
				.setContent(R.id.tab03);
		tabHost.addTab(tab3);
		input = (EditText) findViewById(R.id.input1);
		send = (Button) findViewById(R.id.send1);
		show = (TextView) findViewById(R.id.show1);

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// 如果消息来自子线程
				try {
					if (msg.what == 0x567) {
						// 将读取的内容追加显示在文本框中
						show.append("\n" + msg.obj.toString());
					}
				} catch (Exception e) {
					System.out.println("追加文本到大厅窗口异常");
					e.printStackTrace();
				}
			}
		};

		clientThread = new ClientThread();// 监听服务器发来的消息
		// 客户端启动clientThread线程创建网络连接、读取来自服务器的数据
		new Thread(clientThread).start();


		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					// 当用户按下发送按钮后，将用户输入的数据封装成Message
					// 然后发送给子线程的Handler

					String inputString = input.getText().toString();

					Message msg = new Message();
					msg.what = 0x234;
					msg.obj = "其他用户" + split + inputString;//
					clientThread.receiveHandler.handleMessage(msg);
					// 将发送的内容追加显示在文本框中
					show.append("\n" + HttpUtil.UNAME + ": " + inputString);
					// 清空input文本框
					input.setText("");
				} catch (Exception e) {
					System.out.println("点击发送后报错");
					e.printStackTrace();
				}
			}
		});

		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String entity = bundle.getString("json");
		JSONObject json = null;
		try {
			json = new JSONObject(entity);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		// System.out.println("转化而来的" + json.toString());
		JSONObject user = null;
		String uNickName = null;
		JSONArray friends = null;
		JSONArray groups = null;
		try {
			user = json.getJSONObject("user");
			uNickName = (String) user.get("UNickname");
			HttpUtil.UNAME = uNickName;
			System.out.println(uNickName);
			friends = json.getJSONArray("friends");
			groups = json.getJSONArray("groupList");

		} catch (JSONException e) {
			e.printStackTrace();
		}
		// TextView name = (TextView) findViewById(R.id.name);
		// ImageView image = (ImageView) findViewById(R.id.image);
		// name.setText(uNickName);
		// image.setImageResource(R.drawable.logo);

		expandableListView = (ExpandableListView) findViewById(R.id.list);
		setGroupArray(new ArrayList<String>());
		setChildArray(new ArrayList<List<String>>());
		initdate(groups, friends);
		expandableListView.setAdapter(new MyExpandableListAdapter(
				FragmentTabs.this));

	}

	private void initdate(JSONArray groups, JSONArray friends) {
		JSONObject go = null;
		String GId = null;
		String GName = null;
		List<String> blackList = new ArrayList<String>();
		for (int i = 0; i < groups.length(); i++) {
			try {
				go = groups.getJSONObject(i);
				GId = go.getString("GId");
				GName = go.getString("GName");
				groupArray.add(GName);
				// System.out.println(GName);
				List<String> childItem = new ArrayList<String>();
				for (int j = 0; j < friends.length(); j++) {
					JSONObject fo = null;
					String nickName = null;
					fo = friends.getJSONObject(j);
					try {
						if (fo.getJSONObject("group").getString("GId")
								.equals(GId)) {
							if ((nickName = fo.getJSONObject("userByFriendId")
									.getString("UNickname")) != null)
								childItem.add(nickName
										+ "("
										+ fo.getJSONObject("userByFriendId")
												.getInt("UId") + ")");
							else
								childItem.add(fo
										.getJSONObject("userByFriendId")
										.getString("UName")
										+ "("
										+ fo.getJSONObject("userByFriendId")
												.getInt("UId") + ")");
							// System.out.println(nickName);
						}
					} catch (JSONException e) {
						if (i == 0) {
							if ((nickName = fo.getJSONObject("userByFriendId")
									.getString("UNickname")) != null)
								blackList.add(nickName
										+ "("
										+ fo.getJSONObject("userByFriendId")
												.getInt("UId") + ")");
							else
								blackList.add(fo
										.getJSONObject("userByFriendId")
										.getString("UName")
										+ "("
										+ fo.getJSONObject("userByFriendId")
												.getInt("UId") + ")");
						}
					}
				}
				childArray.add(childItem);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		groupArray.add("黑名单");
		childArray.add(blackList);
	}

	public List<String> getGroupArray() {
		return groupArray;
	}

	public void setGroupArray(List<String> groupArray) {
		this.groupArray = groupArray;
	}

	public List<List<String>> getChildArray() {
		return childArray;
	}

	public void setChildArray(List<List<String>> childArray) {
		this.childArray = childArray;
	}

}